/*
 * functions
 *
 */

--  isworkday
    create or replace function py_iswday(txt text, countrycode text)
        returns text
    as $$
        from pepe.p_datetime.holiday import isWorkDay
        res = ''
        --exceptions.TypeError: len() of unsized object
        -- because you are trying to do len() on something that is probably None
        if txt == None or countrycode == None:
            return res
        else:
            res = isWorkDay(txt, countrycode)
        return res
    $$ language plpythonu;

--  py_regx_onezero
    create or replace function py_regx_onezero(txt text, regx text)
        returns text
    as $$
        import re
        res = '0'
        if txt == None or regx == None:
            return res
        else:
            n = re.search(regx, txt)
            if n: res = '1'
        return res
    $$ language plpythonu;
    -- py_regx_onezero(c013, '^.{1}U')

--  py_regx
    create or replace function py_regx(txt text, regx text)
        returns text
    as $$
        import re
        res = ''
        if txt == None or regx == None:
            return res
        else:
            n = re.search(regx, txt)
            if n: res = n.group(0)
        return res
    $$ language plpythonu;

--  isholiday
    create or replace function py_isholiday(txt text, countrycode text)
        returns text
    as $$
        from pepe.p_datetime.holiday import isHoliday
        res = ''
        res = isHoliday(txt, countrycode)
        return res
    $$ language plpythonu;

--  mvtable
    create or replace function mvtable(src varchar, dest varchar, mask varchar)
    returns void as $$
    declare r record;
    begin
      for r in
              select *
                 from information_schema.tables
                where table_schema = src and table_name like mask
      loop
        execute 'alter table ' || quote_ident(r.table_schema)
                       || '.' || quote_ident(r.table_name)
                       || ' set schema ' || quote_ident(dest);
      end loop;
      return;
    end;
    $$ language plpgsql;

--  py_date2iso
    create or replace function py_date2iso(txt text)
        returns text
    as $$
        from pepe.p_datetime.date_format import date2iso
        res = ''
        res = date2iso(txt)
        return res
    $$ language plpythonu;

--  py_usdate2iso
    CREATE OR REPLACE FUNCTION "public"."py_usdate2iso" ("txt" text)
        RETURNS text AS
    $body$
        from pepe.p_datetime.date_format import usdate2iso, date2iso
        res = ''
        res = usdate2iso(txt)
        return res
    $body$
    LANGUAGE 'plpythonu'
    VOLATILE
    CALLED ON NULL INPUT
    SECURITY INVOKER;
    
--  py_split_part
    create or replace function py_split_part(txt text, sep text, idx numeric)
        returns text
    as $$
        import string, re
        strips = string.split(txt, sep)
        if idx <= (len(strips) - 1):
            res = strips[int(idx)]
            res = re.sub(' {2,}' , ' ', res)
            res = string.strip(res)
            res = string.upper(res)
        else:
            res = ''
        return res
    $$ language plpythonu;

--  py_str2num
    create or replace function py_str2num (a text)
        returns numeric
    AS $$
        from pepe.glib import *
        return float(string2number(args[0]))
    $$ language plpythonu;

--  py_zfill
    create or replace function py_zfill (t text, n numeric)
        returns text
    as $$
        import string 
        return string.zfill(t, n)
    $$ language plpythonu;

--  py_mmddyyyy
    create or replace function py_mmddyyyy (d text)
        returns text
    as $$
        from pepe.glib import *
        d = args[0] 
        return "%s-%s-%s" % (d[6:10], d[:2], d[3:5])
    $$ language plpythonu;



/*
create or replace function py_dd_mm_yyyy (d text)
    returns text
as $$
    from pepe.glib import *
    d = args[0] 
    # 01.34.6789
    if len(d) == 10: 
        return "%s-%s-%s" % (d[6:], d[3:5], d[:2]) 
    else: 
        return ''
$$ language plpythonu;

*/